<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Support\Facades\Cache;
use App\Services\ApplyGoldCoinWithdrawMyBank\ApplyGoldCoinWithdrawMyBank;
use App\Services\ApplyGoldCoinWithdrawMyBank\ApplyGoldCoinWithdrawMyBankRequest;



class WithdrawsMyBankController extends Controller
{
	public function store()
    {
    	$lock = Cache::lock('withdraw', 1);
    	
    	try {

    		if (! $lock->get()) fail('太快了，请再试一次');
			
    		$request = app(ApplyGoldCoinWithdrawMyBankRequest::class);
    		
    		$user = $request->user();
    		

		    $withdraw = $user->withdraws()->where('method','mybank')->orderByDesc('id')->first();
		    
		    if($withdraw){
		    	if($withdraw->status  == 'pending'){
		    		fail('您有审核中的提现记录，不可重复提交');
		    	}
		    }
		    app(ApplyGoldCoinWithdrawMyBank::class, [
			    'user' => $user,
			    'coins' => $request->money,
		    ])->apply();

		    //$user->resetPayCodeTry();
		    
	    } finally {
    		$lock->release();
	    }
	    
	    return response(null, 204);
    }


}
